package cn.chengpengper.im.sdk.application.consumer;

import cn.chengpengper.im.common.domain.constant.ImConstants;
import cn.chengpengper.im.common.domain.enums.ImListenerType;
import cn.chengpengper.im.common.domain.model.ImSendResult;
import cn.chengpengper.im.sdk.infrastructure.multicaster.MessageListenerMulticaster;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Objects;

/*@formatter:off*/

/**
 * ____________________
 * < 神兽护体，永无bug! >
 * --------------------
 *
 * @author huchengpeng
 * @version v1.0.0
 * @date 2024/11/20 00:43
 * @description 接收群聊消息的发送结果数据
 */

/*@formatter:on*/

@Slf4j
@Component
@ConditionalOnProperty(name = "message.mq.type", havingValue = "rocketmq")
@RocketMQMessageListener(consumerGroup = ImConstants.IM_RESULT_PRIVATE_CONSUMER_GROUP, topic = ImConstants.IM_RESULT_PRIVATE_QUEUE)
public class GroupMessageResultConsumer extends BaseMessageResultConsumer implements RocketMQListener<String> {

    @Autowired
    private MessageListenerMulticaster messageListenerMulticaster;

    @Override
    public void onMessage(String message) {
        if (StringUtils.isBlank(message)) {
            log.warn("GroupMessageResultConsumer.onMessage | 接收到的消息为空");
            return;
        }
        ImSendResult<?> imSendResult = this.getResultMessage(message);
        if (Objects.isNull(imSendResult)) {
            log.warn("GroupMessageResultConsumer.onMessage | 转化后的数据为空");
            return;
        }
        messageListenerMulticaster.multicast(ImListenerType.GROUP_MESSAGE, imSendResult);
    }
}
